package com.bn.commerce.pricing;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletException;

import com.bn.commerce.pricing.calculators.BNShippingPricingEngineImpl;

import atg.commerce.order.ShippingGroup;
import atg.commerce.pricing.AvailableShippingMethodsDroplet;
import atg.commerce.pricing.PricingException;
import atg.core.util.ResourceUtils;
import atg.nucleus.naming.ParameterName;
import atg.repository.RepositoryItem;
import atg.service.dynamo.LangLicense;
import atg.service.perfmonitor.PerfStackMismatchException;
import atg.service.perfmonitor.PerformanceMonitor;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;

public class BNAvailableShippingMethodsDroplet extends AvailableShippingMethodsDroplet{

	static final ParameterName SHIPPING_GROUP_PARAM = ParameterName.getParameterName("shippingGroup");
	private static ResourceBundle sResourceBundle = ResourceBundle.getBundle("atg.commerce.pricing.Resources", LangLicense.getLicensedDefault());
	static final ParameterName OUTPUT = ParameterName.getParameterName("output");

	@Override
	public void service(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse) throws ServletException, IOException {

		vlogDebug("BNAvailableShippingMethodsDroplet - Entry");
		String perfName = "service";
		PerformanceMonitor.startOperation("AvailableShippingMethodsDroplet", perfName);
		boolean perfCancelled = false;
		try {
			List availableMethods = null;
			List<RepositoryItem> availableMethodDetails = null;

			if (getShippingPricingEngine() != null) {
				ShippingGroup shippingGroup = getShippingGroup(pRequest, pResponse);

				Collection pricingModels = getPricingModels(pRequest, pResponse);
				RepositoryItem profile = getProfile(pRequest, pResponse);
				Locale locale = getUserLocale(pRequest, pResponse);
				if (shippingGroup != null) {
					try {
						availableMethodDetails = ((BNShippingPricingEngineImpl) getShippingPricingEngine()).getAvailableMethodDetails(shippingGroup, pricingModels, locale, profile, null);
						vlogDebug("availableMethods : " + availableMethods);
					} catch (PricingException exc) {
						try {
							if (!(perfCancelled)) {
								PerformanceMonitor.cancelOperation("AvailableShippingMethodsDroplet", perfName);
								perfCancelled = true;
							}
						} catch (PerfStackMismatchException psm) {
							if (isLoggingWarning()) {
								logWarning(psm);
							}
						}
						if (isLoggingError())
							logError(exc);
					}
				} else {
					String[] args = { SHIPPING_GROUP_PARAM.toString() };
					if (isLoggingError()) {
						logError(ResourceUtils.getMsgResource("missingRequiredInputParam", "atg.commerce.pricing.Resources", sResourceBundle, args));
					}
				}

			}

			pRequest.setParameter("availableMethodDetails", availableMethodDetails);
			pRequest.serviceLocalParameter(OUTPUT, pRequest, pResponse);

			vlogDebug("BNAvailableShippingMethodsDroplet - Exit");

		} finally {
			try {
				if (!(perfCancelled)) {
					PerformanceMonitor.endOperation("AvailableShippingMethodsDroplet", perfName);

					perfCancelled = true;
				}
			} catch (PerfStackMismatchException e) {
				if (isLoggingWarning()) {
					logWarning(e);
				}
			}
		}
	}
}
